package org.chris.quick.widgets;

import android.content.Context;
import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;

/**
 * 解决RecyclerView不能滑动到指定位置
 *
 * @author Chris zou
 * @Date 16/10/12
 * @modifyInfo1 chriszou-16/10/12
 * @modifyContent
 */
public class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {

    public LinearLayoutManagerWithSmoothScroller ( Context context ) {
        super(context, VERTICAL, false);
    }

    public LinearLayoutManagerWithSmoothScroller ( Context context, int orientation, boolean reverseLayout ) {
        super(context, orientation, reverseLayout);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller( recyclerView.getContext ());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    private class TopSnappedSmoothScroller extends LinearSmoothScroller {
        public TopSnappedSmoothScroller (Context context ) {
            super(context);
        }

        @Override
        public PointF computeScrollVectorForPosition ( int targetPosition ) {
            return LinearLayoutManagerWithSmoothScroller.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }
}
